﻿@{
    ViewBag.Title = "Index_App";
    Layout = "~/Views/Shared/_Layout_App.cshtml";
    if(!YJ.Platform.WeiXin.Organize.CheckLogin())
    {
        Response.Write("未登录");
        Response.End();
        return;
    }
}

@{
    Page.BodyStyle = "onload=\"$('#mainform').show();\"";
    string flowid = Request.QueryString["flowid"];
    string instanceid = Request.QueryString["instanceid"];
    string taskid = Request.QueryString["taskid"];
    string stepid = Request.QueryString["stepid"];
    string groupid = Request.QueryString["groupid"];
    string display = Request.QueryString["display"] ?? "0";

    YJ.Platform.WorkFlow bworkFlow = new YJ.Platform.WorkFlow();
    YJ.Platform.WorkFlowTask btask = new YJ.Platform.WorkFlowTask();
    YJ.Platform.WorkFlowButtons bworkFlowButtons = new YJ.Platform.WorkFlowButtons();
    YJ.Platform.AppLibrary bappLibrary = new YJ.Platform.AppLibrary();
    Guid flowID;
    if (!flowid.IsGuid(out flowID))
    {
        Response.Write("流程ID为空!");
        Response.End();
        return;
    }
    var wfInstalled = bworkFlow.GetWorkFlowRunModel(flowID);
    if (wfInstalled == null)
    {
        Response.Write("未找到流程运行时!");
        Response.End();
        return;
    }
    else if (wfInstalled.Status == 3)
    {
        Response.Write("该流程已被卸载,不能发起新的流程实例!");
        Response.End();
        return;
    }
    else if (wfInstalled.Status == 4)
    {
        Response.Write("该流程已被删除,不能发起新的实例!");
        Response.End();
        return;
    }
    Guid stepID;
    if (!stepid.IsGuid(out stepID))
    {
        stepID = wfInstalled.FirstStepID;
    }

    YJ.Data.Model.WorkFlowInstalledSub.Step currentStep = wfInstalled.Steps.ToList().Find(p => p.ID == stepID);

    if (currentStep == null)
    {
        Response.Write("未找到流程步骤!");
        Response.End();
        return;
    }
    Guid taskgid;
    var task = taskid.IsGuid(out taskgid) ? btask.Get(taskgid) : null;
    ViewBag.Title = task != null ? task.Title : wfInstalled.Name;
    Guid currentUserID = YJ.Platform.WeiXin.Organize.CurrentUserID;
    if (task != null && task.ReceiveID != currentUserID && !wfInstalled.InstanceManager.Contains(currentUserID.ToString(), StringComparison.CurrentCultureIgnoreCase))
    {
        Response.Write("您不能查看和处理该任务!");
        Response.End();
        return;
    }
    bool isSign = currentStep.SignatureType == 1 || currentStep.SignatureType == 2;//是否有意见
    int signType = currentStep.SignatureType;
    int isArchives = currentStep.Archives;//是否要归档
    string query = string.Format("flowid={0}&instanceid={1}&taskid={2}&stepid={3}&groupid={4}&appid={5}&tabid={6}&tasktype={7}&signType={8}&display1={9}&isCopyFor={10}&ismobile=1",
        flowID, instanceid, taskid, stepID, groupid, Request.QueryString["appid"], Request.QueryString["tabid"], task == null ? "" : task.Type.ToString(),
        currentStep.SignatureType, display, task != null && task.Type == 5 ? "1" : "0");

    //是否是抄送任务
    bool isCopyFor = false;

    //如果是执行，并且任务ID为GUID,则更新打开时间和状态，检查当前任务是否可以执行。
    if ("0" == display && taskid.IsGuid(out taskgid))
    {
        if (task != null)
        {
            //检查并发
            if (currentStep.Behavior.ConcurrentModel == 1)
            {
                var openTask = btask.GetTaskList(task.ID).Find(p => p.Status == 1 && p.ReceiveID != task.ReceiveID);
                if (openTask != null)
                {
                    Response.Write(string.Format("<script type='text/javascript'>alert('{0}正在处理当前任务，请等待!');top.mainTab.closeTab();</script>", openTask.ReceiveName));
                    Response.End();
                    return;
                }
            }
            
            if ("1" != display && task.Status.In(2, 3, 4, 5))
            {
                Response.Write("<script type='text/javascript'>alert('该任务已处理,请刷新您的待办列表!');top.mainTab.closeTab();</script>");
                Response.End();
                return;
            }
            else if ("1" != display && task.ReceiveID != YJ.Platform.Users.CurrentUserID)
            {
                Response.Write("<script type='text/javascript'>alert('您不能处理当前任务,请刷新您的待办列表!');top.mainTab.closeTab();</script>");
                Response.End();
                return;
            }
            isCopyFor = task.Type == 5;
            btask.UpdateOpenTime(taskgid, YJ.Utility.DateTimeNew.Now, true);
        }
    }

    if (currentStep.Forms.Count() == 0)
    {
        Response.Write("当前步骤未设置表单!");
        Response.End();
        return;
    }

    var form = currentStep.Forms.First();//目前只显示一个表单
    var appLibrary = bappLibrary.Get(form.IDApp.IsEmptyGuid() ? form.ID : form.IDApp, true);
    string src = appLibrary == null ? "" : appLibrary.Address; //bappLibrary.GetFlowRunAddress(appLibrary, query);
    if (src.StartsWith("/"))
    {
        src = Url.Content("~" + src);
    }

    int debugType = wfInstalled.Debug;
    wfInstalled.DebugUsers.RemoveAll(p => p == null);
    bool isDebug = (debugType == 1 || debugType == 2) && wfInstalled.DebugUsers.Exists(p => p.ID == YJ.Platform.Users.CurrentUserID);
    bool isCustomeForm = appLibrary != null && appLibrary.Code.IsNullOrEmpty();//是否是自定义表单
    
}
<form id="mainform" name="mainform" method="post" target="submiter" style="display:none;">
    @Html.AntiForgeryToken()
    @if ("0" == display)//display为0表示执行，1表示查看
    {
    <div class="toolbar" style="margin-top:0; border-top:none 0; position:fixed; top:0; left:0; right:0; margin-left:auto; z-index:999; width:100%; margin-right:auto;">
        <div>
        @{
        //如果是抄送，只显示完成按钮
        List<YJ.Data.Model.WorkFlowInstalledSub.StepSet.Button> buttons = new List<YJ.Data.Model.WorkFlowInstalledSub.StepSet.Button>();
        if (isCopyFor)
        {
            buttons.Add(new YJ.Data.Model.WorkFlowInstalledSub.StepSet.Button()
            {
                ID = "517cd156-cf5a-404c-8a99-e784c1a3435a",
                Note = "",
                Sort = 0
            });
        }
        else
        {
            foreach (var button in currentStep.Buttons)
            {
                buttons.Add(button);
            }
        }
        foreach (var button in buttons)
        {
            if (button == null)
            {
                continue;
            }
            Guid buttonID;
            if (button.ID.IsGuid(out buttonID))
            {
                var button1 = bworkFlowButtons.Get(buttonID, true);
                if (button1 == null)
                {
                    continue;
                }
                var funName = string.Concat("fun_", button1.ID.ToString("N"), "()");
                string showTitle = button.ShowTitle.IsNullOrEmpty() ? button1.Title : button.ShowTitle.Trim();
            <a href="javascript:void(0);" onclick="@funName;return false;" title="@(button1.Note.IsNullOrEmpty() ? "" : button1.Note.Replace("\"", "'"))">
                @if(button1.Ico.IsFontIco())
                {
                <i class='fa @button1.Ico' style='font-size:16px;vertical-align:middle;margin-right:3px;'></i><label>@showTitle</label>
                }
                else
                {
                <span style="background:url(@Url.Content("~" + button1.Ico)) no-repeat left center;">@showTitle</span>
                }
            </a>
            <script type="text/javascript">
                function @funName {@Html.Raw(button1.Script)}
            </script>
            }
            else if (string.Compare(button.ID, "other_splitline", true) == 0)//显示其它特定按钮如分隔线|
            { 
            <span class="toolbarsplit">&nbsp;</span>
            }
        }
        }
        </div>
    </div>
    <div style="height:40px;"></div>
        
        
    
        
    
    <input type="hidden" name="instanceid" id="instanceid" value="" />
    <input type="hidden" name="params" id="params" value="" />
        if (isDebug && debugType == 1)
        {
    <br /><br />
    <iframe name="submiter" style="width:99%; height:200px; border:1px solid #666; margin-left:4px; margin-top:10px; overflow:auto;"></iframe>
        }
        else
        {
    <iframe name="submiter" style="width:99%; height:1px; margin:0; display:none;"></iframe>    
        }
    }

        
    @if (task != null && task.Type == 7 && !task.Note.IsNullOrEmpty()) //显示加签备注信息
    {
        <div style='margin:0 auto;text-align:center;color:red;font-weight:bold;font-size:14px;'><br/><br/><br/>@task.Note</div>
    }

    <!--表单主体--> 
    <div style="@(isCustomeForm ? "" : "margin:8px;")" id="form_body_div">
    @{
        if (!src.IsNullOrEmpty())
        {
            if (!isCustomeForm)
            {
                var extName = System.IO.Path.GetExtension(src).ToLower();
                if (extName.StartsWith(".cshtml"))
                {
                    @RenderPage(src)
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    System.IO.TextWriter tw = new System.IO.StringWriter(sb);
                    Server.Execute(src, tw);
                    @Html.Raw(sb.ToString().RemovePageTag())
                }
            }
            else
            {
                <input type="hidden" id="Form_TitleField" name="Form_TitleField" value="customformtitle" />
                <input type="hidden" id="customformtitle" name="customformtitle" value="" />
                <input type="hidden" id="Form_AutoSaveData" name="Form_AutoSaveData" value="0" />
                src = src.IndexOf('?') >= 0 ? src + Request.Url.Query.Substring(1) : src + Request.Url.Query;
                <iframe src="@(src.StartsWith("/") ? WebMvc.Common.Tools.BaseUrl + src : "" + src)" id="customeformiframe" style="border:none 0;padding:0;width:100%;margin-top:28px;"></iframe>
            }
        }
        }
    </div>
    <!--表单主体--> 

    <!--签批意见栏-->
    @if (isSign && "0" == display && !isCopyFor){
        string commentsOptions = new YJ.Platform.WorkFlowComment().GetOptionsStringByUserID(YJ.Platform.Users.CurrentUserID);
        <div style="width:98%; margin: 0 auto;">
            <div style="margin:8px 0 0 0;">
                <div style="margin-bottom:4px;">处理意见：</div>
                <div>
                <select class="myselect" id="mycomment" style="margin-right:4px; width:80px;" onchange="$('#comment').val(this.value);"><option value=""></option>@Html.Raw(commentsOptions)</select><input type="text" class="mytext" id="comment" name="comment" value="" style="width:50%; margin-right:1px;" />
                @if (signType == 2){
                <input type="hidden" value="" id="issign" name="issign" /><input type="button" class="mybutton" id="signbutton" onclick="sign();" value="&nbsp;&nbsp;签章&nbsp;&nbsp;"/>
        
                    string signFile = string.Concat(Server.MapPath(Url.Content("~/Content/UserSigns/")), currentUserID, ".gif");
                    string signSrc = string.Concat(Url.Content("~/Content/UserSigns/"), currentUserID, ".gif");
                    if (!System.IO.File.Exists(signFile))
                    {
                        System.Drawing.Bitmap img = new YJ.Platform.WorkFlow().CreateSignImage(YJ.Platform.WeiXin.Organize.CurrentUserName);
                        if (img != null)
                        {
                            img.Save(signFile, System.Drawing.Imaging.ImageFormat.Gif);
                        }
                    }
                    <img alt="" src="@signSrc" id="signimg" style="vertical-align:middle; display:none;" />
                }
                </div>
            </div>
        </div>
        <div style="height:40px;"></div>
    }
    <!--签批意见栏-->

    <!--历史意见显示-->
    <div id="form_commentlist_div">
    @if (currentStep.OpinionDisplay == 1)//如果步骤设置为要显示意见
    {
        @RenderPage("ShowComment.cshtml")
    }
    </div>
    <!--历史意见显示-->

    <!--归档内容-->
    <textarea id="form_body_div_textarea" name="form_body_div_textarea" style="display:none;"></textarea>
    <textarea id="form_commentlist_div_textarea" name="form_commentlist_div_textarea" style="display:none;"></textarea>
    <!--归档内容-->
    <br /><br />
</form>
<script type="text/javascript">
    var isDebug = '@isDebug' == 'True' && '1' == '@debugType';
    var isSign = '@isSign' == 'True';
    var signType = '@signType';
    var iframeid = '@Request.QueryString["tabid"]';
    var isShow = "0" != "@display";//是否是查看模式
    var appid = '@Request.QueryString["appid"]';
    var query = '@Html.Raw(query)';
    var isSystemDetermine = '@(currentStep.Behavior.FlowType == 0 ? "1" : "0")';//当前步骤的后续流转类型是否是系统判断
    var instanceid='@instanceid';
    var isCustomeForm = '@(isCustomeForm ? "1" : "0")';
    var ismobile = true;
    if(!top.mainDialog)
    {
        top.mainDialog = new RoadUI.Window();
    }
    $(function(){
        if("1" == "@isArchives")
        {
            $("#form_body_div_textarea").val($("#form_body_div").html());
            $("#form_commentlist_div_textarea").val($("#form_commentlist_div").html());
        }
        if("1"==isCustomeForm)
        {
            $("#customeformiframe").height($(window).height()-35);
        }
    });
</script>
<script type="text/javascript" src="~/Scripts/FlowRun/common.js" ></script>
